///
/// Override Validate method. If record exists, do not add the record and throw
/// custom error message to the user.
/// When an exception is thrown in the Validate() method, it is caught in the SaveButton_Click event handler.
/// SaveButton_Click event handler calls RegisterJScriptAlert to report the error message to the user.
/// Throwing an exception ensures that the record will not be saved. If the exception is
/// not thrown the record is saved.
///
public override void Validate()
{
// Create a whereStr
string whereStr = ${${Table Name}ClassName}.${Primary Key}.InternalName + "='" + this.${Primary Key Control}.Text + "'";
${${Table Name}RecordClassName} rec = null;
// Check if the record exists
rec = ${${Table Name}ClassName}.GetRecord(whereStr);
// If record exists then throw custom error message to the user
if (!(rec == null))
{
// Set custom error message
throw new Exception("Record with this primary key already exists.");
}
}
|